package com.udaos.modules.card.services;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Dao;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.udaos.common.base.Service;
import com.udaos.models.card.Ud_card;
import com.udaos.models.card.Ud_card_template;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.card.enums.CardTypeEnums;

/**
 * 优惠券Service
 * @author Administrator
 *
 */
@IocBean(args = { "refer:dao" })
public class UdCardTemplateService extends Service<Ud_card_template>{

	@Inject
	private UdCardService cardService;
	
	public UdCardTemplateService(Dao dao) {
		super(dao);
	}

	/**
	 * 保存平台卡券
	 * @param coupon_model
	 * @param coupon_begin_timestamp_str
	 * @param coupon_end_timestamp_str
	 * @throws ParseException 
	 */
	@Aop(TransAop.READ_COMMITTED)
	public void save(Ud_card_template coupon_model, String coupon_begin_timestamp_str, String coupon_end_timestamp_str) throws ParseException {
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		//设置状态
		coupon_model.setState("0");
		//设置ID
		coupon_model.setId(UUID.randomUUID().toString().replaceAll("-", ""));
		//设置单位信息
		coupon_model.setCompany_id(user.getUnit().getCompany_id());
		//设置时间
		if(coupon_model.getCoupon_date_type().equals("0")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int coupon_begin_timestamp = (int) (sdf.parse(coupon_begin_timestamp_str).getTime() / 1000);
			int coupon_end_timestamp=(int) (sdf.parse(coupon_end_timestamp_str).getTime() / 1000);
			
			coupon_model.setCoupon_begin_timestamp(coupon_begin_timestamp);
			coupon_model.setCoupon_end_timestamp(coupon_end_timestamp);
			//设置失效时间
			coupon_model.setCoupon_lose_date(coupon_end_timestamp);
		}else{
			//设置失效时间
			 long time = System.currentTimeMillis();  
		     Date d = new Date(time);  
		     Calendar c = Calendar.getInstance();  
		     c.setTime(d);  
		     c.add(Calendar.DAY_OF_MONTH, coupon_model.getCoupon_fixed_term());  
		     coupon_model.setCoupon_lose_date((int)(c.getTimeInMillis()/1000));
		}
		//设置创建时间
		coupon_model.setCoupon_create_date((int) (System.currentTimeMillis() / 1000));
		dao().insert(coupon_model);
		
		//同步到Ud_card中
		Ud_card card=new Ud_card();
		card.setCompany_id(coupon_model.getCompany_id());
		card.setCreateAt((int) (System.currentTimeMillis() / 1000));
		card.setRawid(coupon_model.getId());
		card.setSource(CardTypeEnums.PLATFORM.getCode());
		card.setCreateBy(user.getId());
		card.setState(0);
		
		cardService.insert(card);
	}

	/**
	 * 修改平台卡券
	 * @param coupon_model
	 * @param coupon_begin_timestamp_str
	 * @param coupon_end_timestamp_str
	 * @throws ParseException 
	 */
	@Aop(TransAop.READ_COMMITTED)
	public void updateCoupon(Ud_card_template coupon_model, String coupon_begin_timestamp_str,
			String coupon_end_timestamp_str) throws Exception {
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		//设置单位信息
		coupon_model.setCompany_id(user.getUnit().getCompany_id());
		//设置创建时间
		coupon_model.setCoupon_create_date((int) (System.currentTimeMillis() / 1000));
		//设置时间
		if(coupon_model.getCoupon_date_type().equals("0")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			int coupon_begin_timestamp = (int) (sdf.parse(coupon_begin_timestamp_str).getTime() / 1000);
			int coupon_end_timestamp=(int) (sdf.parse(coupon_end_timestamp_str).getTime() / 1000);
			
			coupon_model.setCoupon_begin_timestamp(coupon_begin_timestamp);
			coupon_model.setCoupon_end_timestamp(coupon_end_timestamp);
			//设置失效时间
			coupon_model.setCoupon_lose_date(coupon_end_timestamp);
		}else{
			//设置失效时间
			 long time = System.currentTimeMillis();  
		     Date d = new Date(time);  
		     Calendar c = Calendar.getInstance();  
		     c.setTime(d);  
		     c.add(Calendar.DAY_OF_MONTH, coupon_model.getCoupon_fixed_term());  
		     coupon_model.setCoupon_lose_date((int)c.getTimeInMillis()/1000);
		}
		
		dao().update(coupon_model);
	}

	
}
